/**
 * 
 */
package de.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.dd.DD;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.CycleButton;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Resizable;
import com.gwtext.client.widgets.ResizableConfig;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.CycleButtonListenerAdapter;
import com.gwtext.client.widgets.event.ResizableListenerAdapter;
import com.gwtext.client.widgets.event.TabPanelListenerAdapter;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.CheckItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;

/**
 * @author engler
 * 
 */
public class HelloWorld implements EntryPoint {

	private TabPanel tabPanel;
	private int index;
	private Menu menu;

	public void onModuleLoad() {
		final Panel mainPanel = new Panel();
		mainPanel.setTitle("Draggable");
		mainPanel.setBorder(true);
		mainPanel.setWidth(200);
		mainPanel.setCollapsible(true);
		mainPanel.setHtml(getBogusMarkup());

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSpacing(15);
		mainPanel.setTitle("Ressizable Panel");  
		mainPanel.setIconCls("paste-icon");  
		mainPanel.setWidth(200);  
		mainPanel.setCollapsible(true);  
		mainPanel.setHtml(getBogusMarkup());  
		   
		         ResizableConfig config = new ResizableConfig();  
		         config.setHandles(Resizable.SOUTH_EAST);  
		   
		         final Resizable resizable = new Resizable(mainPanel, config);  
		         resizable.addListener(new ResizableListenerAdapter() {  
		             public void onResize(Resizable self, int width, int height) {  
		            	 mainPanel.setWidth(width);  
		            	 mainPanel.setHeight(height);  
		             }  
		         });  

		
			
		mainPanel.setTitle("Hello World!");
		mainPanel.setHeight(300);
		mainPanel.setWidth(500);
		mainPanel.setFrame(true);
		mainPanel.setHtml("<p>This is my first GWT-Ext webpage and it was a breeze to design!</p>");
		mainPanel.setStyle("margin: 10px 0px 0px 10px;");
			
		
		CycleButton button = new CycleButton();
		button.setShowText(true);
		button.setPrependText("View as ");

		// add CheckItem's to the CycleButton
		button.addItem(new CheckItem("text only", true));
		button.addItem(new CheckItem("HTML", false));

		// log check item changes
		button.addListener(new CycleButtonListenerAdapter() {
			public void onChange(CycleButton self, CheckItem item) {
				System.out.println(item.getText() + " selected.");
			}
		});
		mainPanel.add(button);

		Panel verticalPanel = new Panel();
		verticalPanel.setLayout(new VerticalLayout(15));

		Button button2 = new Button("Add Tab", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				Panel tab = addTab();
				tabPanel.activate(tab.getId());
				tabPanel.scrollToTab(tab, true);
			}
		});
		button2.setIconCls("new-tab-icon");
		mainPanel.add(button2);

		tabPanel = new TabPanel();
		tabPanel.setResizeTabs(true);
		tabPanel.setMinTabWidth(115);
		tabPanel.setTabWidth(135);
		tabPanel.setEnableTabScroll(true);
		tabPanel.setWidth(450);
		tabPanel.setHeight(250);
		tabPanel.setActiveTab(0);

		tabPanel.addListener(new TabPanelListenerAdapter() {
			public void onContextMenu(TabPanel source, Panel tab, EventObject e) {
				showMenu(tab, e);
			}
		});

		for (index = 0; index < 7; index++) {
			addTab();
		}
		
		 DD dd = new DD(mainPanel);  
		   
		         horizontalPanel.add(mainPanel);  
		   
		         mainPanel.add(horizontalPanel); 
		
		
		mainPanel.add(tabPanel);

		//mainPanel.add(verticalPanel);


		RootPanel.get().add(mainPanel);
	}

	private void showMenu(final Panel tab, EventObject e) {
		if (menu == null) {
			menu = new Menu();
			Item close = new Item("Close Tab");
			close.setId("close-tab-item");
			close.addListener(new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
					tabPanel.remove(tabPanel.getActiveTab());
				}
			});
			menu.addItem(close);

			Item closeOthers = new Item("Close Other Tabs");
			closeOthers.setId("close-others-item");
			closeOthers.addListener(new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
					Component[] items = tabPanel.getItems();
					for (int i = 0; i < items.length; i++) {
						Component component = items[i];
						if (!component.getId().equals(
								tabPanel.getActiveTab().getId())) {
							tabPanel.remove(component);
						}
					}
				}
			});
			menu.addItem(closeOthers);
		}

		BaseItem closeOthers = menu.getItem("close-others-item");
		if (tabPanel.getItems().length == 1) {
			closeOthers.disable();
		} else {
			closeOthers.enable();
		}
		menu.showAt(e.getXY());
	}

	private Panel addTab() {
		Panel tab = new Panel();
		tab.setAutoScroll(true);
		tab.setTitle("New Tab " + (++index));
		tab.setIconCls("tab-icon");
		tab.setHtml("Tab Body " + index + "<br/><br/>" + getBogusMarkup());
		tab.setClosable(true);
		tabPanel.add(tab);
		return tab;
	}

	private static String getBogusMarkup() {
		return "<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. "
				+ "Sed metus nibh, sodales a, porta at, vulputate eget, dui.  "
				+ "In pellentesque nisl non sem. Suspendisse nunc sem, pretium eget, "
				+ "cursus a, fringilla vel, urnaa.";
	}
	
}